package net.vinote.smart.platform.module.mail;

import java.security.Security;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

public class EmailReceive {
	private Properties props = new Properties();
	private Store store;

	public void connect(String user, String password, String host, String port,
			String protocol, boolean ssl) throws MessagingException {

		props.setProperty("mail." + protocol + ".host", host); // 指定SMTP服务器
		props.setProperty("mail." + protocol + ".port", port);
		props.setProperty("mail.store.protocol", protocol);
		if (ssl) {
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			props.setProperty("mail." + protocol + ".socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");
			props.setProperty("mail." + protocol + ".socketFactory.fallback",
					"false");
			props.setProperty("mail." + protocol + ".socketFactory.port", port);
		}
		Session session = Session.getInstance(props);
		store = session.getStore(protocol);
		store.connect(user, password);
	}

	// public void send(String from, String to, String subject, String content)
	// {
	// Properties props = new Properties();
	// props.put("mail.smtp.host", host); // 指定SMTP服务器
	// props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证
	// try {
	// Session mailSession = Session.getDefaultInstance(props);
	// Message message = new MimeMessage(mailSession);
	// message.setFrom(new InternetAddress(from)); // 发件人
	// message.addRecipient(Message.RecipientType.TO, new InternetAddress(
	// to)); // 收件人
	// message.setSubject(subject); // 邮件主题
	// message.setText(content); // 邮件内容
	// message.saveChanges();
	// Transport transport = mailSession.getTransport("smtp");
	// transport.connect(host, user, password);
	// transport.sendMessage(message, message.getAllRecipients());
	// transport.close();
	// } catch (Exception e) {
	// System.out.println(e);
	// }
	// }

	public Message[] list() throws MessagingException {
		Folder folder = store.getFolder("INBOX");
		folder.open(Folder.READ_WRITE);
		return folder.getMessages();
	}

	public void close() {
		try {
			store.close();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws MessagingException {
		EmailReceive rece = new EmailReceive();
		/*
		 * rece.connect("zhengjunweimail@163.com", "18268134120",
		 * "imap.163.com", "993", "imap", true); rece.list(); rece.close();
		 * 
		 * rece.connect("zhengjunweimail@163.com", "18268134120",
		 * "imap.163.com", "143", "imap", false); rece.list(); rece.close();
		 * 
		 * rece.connect("zhengjunweimail@163.com", "18268134120", "pop.163.com",
		 * "995", "pop3", true); rece.list(); rece.close();
		 * 
		 * rece.connect("zhengjunweimail@163.com", "18268134120", "pop.163.com",
		 * "110", "pop3", false); rece.list(); rece.close();
		 */

		rece.connect("501190241@qq.com", "junweiis008", "pop.qq.com", "995",
				"pop3", true);
		rece.list();
		rece.close();

		rece.connect("501190241@qq.com", "junweiis008", "imap.qq.com", "993",
				"imap", true);
		rece.list();
		rece.close();
	}
}